<template>
  <div class="category-container">
    <div class="category-wrapper">
      <!-- 分类项循环 -->
      <div
          v-for="(category, index) in visibleCategories"
          :key="index"
          class="category-item"
          @click="handleCategoryClick(category)"
      >
        <div class="icon-wrapper">
          <!-- 改为组件方式渲染图标 -->
          <component
              :is="category.icon"
              class="icon"
          ></component>
          <div class="icon-bg"></div>
        </div>
        <span class="category-name">{{ category.name }}</span>
        <div class="hover-indicator"></div>
      </div>
      <!-- 更多分类项 -->
      <div
          v-if="showMore"
          class="category-item more-item"
          @click="handleMoreClick"
      >
        <div class="icon-wrapper">
          <!-- 更多图标也改为组件方式 -->
          <component
              is="More"
              class="icon"
          ></component>
          <div class="icon-bg"></div>
        </div>
        <span class="category-name">更多分类</span>
        <div class="hover-indicator"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, defineOptions } from 'vue'
import { useRouter } from 'vue-router'
import Request from '@/utils/request.js'
// 无需单独导入图标组件（已全局注册）

defineOptions({
  name: 'FrontCategory'
})

const categories = ref([])
const maxVisible = ref(6)
const router = useRouter()

// 可见分类列表
const visibleCategories = computed(() => {
  return categories.value.slice(0, maxVisible.value)
})

// 是否显示更多分类
const showMore = computed(() => {
  return categories.value.length > maxVisible.value
})

// 分类图标映射（返回Element Plus图标组件名，首字母大写）
const getCategoryIcon = (name) => {
  // 映射表值为Element Plus图标组件名（与全局注册名称一致）
  const iconMap = {
    '蔬菜': 'Food',         // 对应<Food />组件
    '水果': 'Apple',        // 对应<Apple />组件
    '谷物': 'Dish',         // 对应<Dish />组件
    '干货': 'Dessert',      // 对应<Dessert />组件
    '肉类': 'Meat',         // 对应<Meat />组件（需确认是否存在，不存在可替换）
    '水产': 'Fish',         // 对应<Fish />组件
    '农副产品': 'Sugar',    // 对应<Sugar />组件
    '其他': 'More'          // 对应<More />组件
  }
  return iconMap[name] || 'More' // 默认为More图标
}

// 获取分类数据
const fetchCategories = async () => {
  try {
    const res = await Request.get('/category/all')
    if (res.code === '0') {
      categories.value = res.data.map(item => ({
        ...item,
        // 优先使用接口返回的icon（组件名），否则用名称映射
        icon: item.icon || getCategoryIcon(item.name)
      }))
    }
  } catch (error) {
    console.error('获取分类数据失败:', error)
  }
}

// 分类点击事件
const handleCategoryClick = (category) => {
  router.push({
    name: 'category',
    params: { id: category.id },
    query: { name: category.name }
  })
}

// 更多分类点击事件
const handleMoreClick = () => {
  router.push('/products')
}

// 组件挂载时获取数据
onMounted(() => {
  fetchCategories()
})
</script>

<style scoped>
/* 样式保持不变，图标样式通过class控制 */
.category-container {
  background: linear-gradient(to right, #ffffff, #f8faf5);
  padding: 16px 0;
  margin: 16px 0;
  border-radius: 16px;
  position: relative;
  overflow: hidden;
}

.category-container::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
      radial-gradient(circle at 0% 0%, rgba(103, 194, 58, 0.03) 0%, transparent 50%),
      radial-gradient(circle at 100% 0%, rgba(103, 194, 58, 0.03) 0%, transparent 50%);
  opacity: 0.8;
  z-index: 0;
}

.category-wrapper {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  position: relative;
  z-index: 1;
}

.category-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 8px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.category-item:hover {
  background: rgba(255, 255, 255, 0.8);
  transform: translateY(-2px);
}

.icon-wrapper {
  width: 52px;
  height: 52px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 8px;
}

/* 图标样式控制（组件形式的图标会继承这些样式） */
.icon {
  font-size: 24px;
  color: #67C23A;
  position: relative;
  z-index: 2;
  transition: all 0.3s ease;
}

.icon-bg {
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg,
  rgba(103, 194, 58, 0.1),
  rgba(103, 194, 58, 0.15)
  );
  border-radius: 12px;
  transition: all 0.3s ease;
}

.category-item:hover .icon-bg {
  transform: scale(1.1) rotate(-5deg);
  background: linear-gradient(135deg,
  rgba(103, 194, 58, 0.15),
  rgba(103, 194, 58, 0.2)
  );
}

.category-item:hover .icon {
  transform: scale(1.1);
}

.category-name {
  font-size: 14px;
  color: #2c3e50;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
}

.hover-indicator {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0;
  height: 2px;
  background: #67C23A;
  transition: all 0.3s ease;
  transform: translateX(-50%);
}

.category-item:hover .hover-indicator {
  width: 20px;
}

/* 更多分类项样式 */
.more-item .icon-bg {
  background: linear-gradient(135deg,
  rgba(144, 147, 153, 0.1),
  rgba(144, 147, 153, 0.15)
  );
}

.more-item .icon {
  color: #909399;
}

.more-item:hover .icon-bg {
  background: linear-gradient(135deg,
  rgba(144, 147, 153, 0.15),
  rgba(144, 147, 153, 0.2)
  );
}

.more-item:hover .hover-indicator {
  background: #909399;
}

/* 响应式样式 */
@media (max-width: 768px) {
  .category-wrapper {
    flex-wrap: wrap;
  }

  .category-item {
    flex: 0 0 calc(25% - 9px);
  }

  .icon-wrapper {
    width: 44px;
    height: 44px;
  }

  .icon {
    font-size: 20px;
  }

  .category-name {
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .category-item {
    flex: 0 0 calc(33.33% - 8px);
  }

  .icon-wrapper {
    width: 40px;
    height: 40px;
  }

  .icon {
    font-size: 18px;
  }
}
</style>
